import {createStore} from "vuex"

export default createStore({
    state: {
        loadingBarCount: 0, // 顶部进度条 - 引用计数
        menus: [], // 菜单
        isLoad: false,
        breadcrumb: [], // 面包屑
        themeMode: "", // 主题模式
        themeColor: "", // 主题颜色
    },
    mutations: {
        // 顶部进度条 - 开始
        loadingBarStart(state) {
            if (state.loadingBarCount === 0) {
                window.$loadingBar?.start()
            }
            state.loadingBarCount++
        },
        // 顶部进度条 - 结束
        loadingBarFinish(state) {
            state.loadingBarCount--
            if (state.loadingBarCount <= 1) {
                window.$loadingBar?.finish()
                state.loadingBarCount = 0
            }
        },
        // 顶部进度条 - 错误
        loadingBarError(state) {
            state.loadingBarCount = 0
            window.$loadingBar?.error()
        },
        // 设置菜单
        setMenus(state, menus) {
            state.menus = menus
        },
        // 设置是否加载中
        setIsLoad(state, isLoad) {
            state.isLoad = isLoad
        },
        // 设置面包屑
        setBreadcrumb(state, breadcrumb) {
            state.breadcrumb = breadcrumb
        },
        // 设置主题模式
        setThemeMode(state, themeMode) {
            state.themeMode = themeMode
        },
        // 切换主题
        toggleTheme(state) {
            let mode = localStorage.getItem("themeMode") === "light" ? "dark" : "light"

            localStorage.setItem("themeMode", mode)
            state.themeMode = mode
        },
        // 设置主题颜色
        setThemeColor(state, themeColor) {
            state.themeColor = themeColor
        },
        // 改变主题色
        changeThemeColor(state, color) {
            localStorage.setItem("themeColor", color)
            state.themeColor = color
        },
    },
    actions: {
        // 初始化菜单
        initMenu({commit}, response) {
            commit("setMenus", response)
        },
        // 需要加载
        needLoad({commit}) {
            commit("setIsLoad", false)
        },
        // 加载完成
        loaded({commit}) {
            commit("setIsLoad", true)
        },
    },
    modules: {},
})
